package com.zr.gdms.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zr.gdms.dao.TeacherInfoDao;
import com.zr.gdms.model.Teacher;
import com.zr.gdms.util.JDBCUtil;
/**
 * 老师信息管理dao层实现,逻辑代码实现
 * @author xiaRenAn
 *
 */
public class TeacherInfoDaoImpl implements TeacherInfoDao {

	@Override
	public List<Teacher> allTeacherinfoByUid(int uid) {
		 List<Teacher> lt = new ArrayList<Teacher>();
         StringBuffer str = new StringBuffer("");
         str.append(" SELECT teacher.tid,teacher.tname,teacher.tdepart,teacher.tprofe,teacher.tage,teacher.tsex,teacher.ttel");
         str.append(" FROM teacher");
         str.append(" INNER JOIN `user`");
         str.append(" ON teacher.uid=`user`.uid");
         str.append(" WHERE `user`.uid= ?");
         Connection con = JDBCUtil.getConnection();
         PreparedStatement pst;
         try {
			pst = con.prepareStatement(str.toString());
			pst.setInt(1, uid);
			ResultSet rs = pst.executeQuery();
			while (rs.next()) {
				Teacher t  = new Teacher();
				  t.setTid(rs.getInt("tid"));
				  t.setTname(rs.getString("tname"));
				  t.setTdepart(rs.getString("tdepart"));
				  t.setTprofe(rs.getString("tprofe"));
				  t.setTage(rs.getString("tage"));
				  t.setTsex(rs.getString("tsex"));
				  t.setTtel(rs.getString("ttel"));
				  lt.add(t);
				  
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return lt;
	}

	@Override
	public boolean updateTeacherInfo(int tid, String tname, String tdepart, String tprofe, int tage, String tsex,
			String ttel) {
		   boolean flag = false;
		   StringBuilder str = new StringBuilder("");
		   str.append(" UPDATE teacher SET tname=?,tdepart= ?,tprofe= ?,tage= ?,tsex= ?,ttel= ?");
		   str.append(" WHERE teacher.tid = ?");
		   Connection con = JDBCUtil.getConnection();
		   PreparedStatement pst;
		   try {
			pst = con.prepareStatement(str.toString());
			pst.setString(1, tname);
			pst.setString(2, tdepart);
			pst.setString(3, tprofe);
			pst.setInt(4, tage);
			pst.setString(5, tsex);
			pst.setString(6, ttel);
			pst.setInt(7, tid);
			int i = pst.executeUpdate();
			if (i>0) {
				flag = true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}

}
